package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.Category;
import com.example.vo.CategoryListVO;
import com.example.vo.CategoryPopularVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * <p>
 * 分类表 Mapper 接口
 * </p>
 *
 * @author wuyunbin
 * @since 2024-10-15
 */
@Mapper
public interface CategoryMapper extends BaseMapper<Category> {
    
    @Select("SELECT c.*, COUNT(a.id) as article_count " +
            "FROM tb_category c " +
            "LEFT JOIN tb_article a ON c.id = a.category_id " +
            "GROUP BY c.id " +
            "ORDER BY c.create_time DESC")
    List<CategoryListVO> selectCategoryWithArticleCount();

    /**
     * 获取热门分类（根据包含的文章数量）
     * @param limit 返回的分类数量
     * @return 热门分类列表
     */
    List<CategoryPopularVO> findPopularCategories(@Param("limit") int limit);
}
